<?php

/**
 * Pes for PHP 5.3+
 *
 * Copyright (c) 2013 phpeskill.com (http://www.pescms.com)
 *
 * For the full copyright and license information, please view
 * the file LICENSE.md that was distributed with this source code.
 */

namespace App\Func;

use Model\Admin\CategoryModel as CategoryModel;
use Core\Db\Db as Db;

/**
 * 公共函数库
 * @author LazyPHP
 * @version 1.0
 */
class Func extends Form {

    /**
     * 获取状态
     * @param type $code 状态码
     * @return string 返回状态
     */
    function status($code) {
        switch ($code) {
            case '0':
                return '<font color="red">禁用</font>';
                break;
            case '1':
                return '<font color="blue">启用</font>';
                break;
        }
    }

    /**
     * 字符串截断
     * @param type $sourcestr 字符串
     * @param type $cutlength 截断的长度
     * @param type $suffix 截断后显示的内容
     * @return string 返回一个截断后的字符串
     */
    function str_cut($sourcestr, $cutlength, $suffix = '...') {
        $str_length = strlen($sourcestr);
        if ($str_length <= $cutlength) {
            return $sourcestr;
        }
        $returnstr = '';
        $n = $i = $noc = 0;
        while ($n < $str_length) {
            $t = ord($sourcestr[$n]);
            if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
                $i = 1;
                $n++;
                $noc++;
            } elseif (194 <= $t && $t <= 223) {
                $i = 2;
                $n += 2;
                $noc += 2;
            } elseif (224 <= $t && $t <= 239) {
                $i = 3;
                $n += 3;
                $noc += 2;
            } elseif (240 <= $t && $t <= 247) {
                $i = 4;
                $n += 4;
                $noc += 2;
            } elseif (248 <= $t && $t <= 251) {
                $i = 5;
                $n += 5;
                $noc += 2;
            } elseif ($t == 252 || $t == 253) {
                $i = 6;
                $n += 6;
                $noc += 2;
            } else {
                $n++;
            }
            if ($noc >= $cutlength) {
                break;
            }
        }
        if ($noc > $cutlength) {
            $n -= $i;
        }
        $returnstr = substr($sourcestr, 0, $n);


        if (substr($sourcestr, $n, 6)) {
            $returnstr = $returnstr . $suffix; //超过长度时在尾处加上省略号
        }
        return $returnstr;
    }

    /**
     * 是否存在略缩图
     * @param type $img
     */
    public function thumb($img) {
        if (is_file('.' . $img)) {
            return $img;
        } else {
            return '/img/lose.png';
        }
    }

    /**
     * 获取分类名称
     */
    public function getCateName($id) {
        $result = CategoryModel::getCategory($id);
        return $result['cate_name'];
    }

    /**
     * 生成令牌
     * @return type
     */
    public function token() {
        $token = uniqid("pes" . time());
        $_SESSION['token'] = $token;
        return $token;
    }

    /**
     * 生成URL链接
     * @param type $controller 链接的控制器
     * @param array $param 参数
     * @return type 返回URL
     */
    public function url($controller, array $param = array()) {
        $db = Db::__init();
        $db->tableName('option');
        $result = $db->where('option_name = "urlModel"')->find();
        $urlModel = json_decode($result['value'], true);
        $url = '';

        if ($urlModel['index'] == '0') {
            $url .= '/index.php/';
        } else {
            $url .= '/';
        }

        $dismantling = explode('-', $controller);
        $totalDismantling = count($dismantling);

        if ($totalDismantling == 2) {
            switch ($urlModel['urlModel']) {
                case '2':
                    $url .= implode('-', $dismantling);
                    $url .= $this->urlLinkStr($param, '-');
                    break;
                case '3':
                    $url .= implode('/', $dismantling);
                    $url .= $this->urlLinkStr($param, '/');
                    break;
                case '1':
                default:
                    $url = $urlModel['index'] == '0' ? '/index.php' : '/';
                    $url .= "?m={$dismantling[0]}&a={$dismantling[1]}";
                    $url .= $this->urlLinkStr($param);
            }
        } else {
            switch ($urlModel['urlModel']) {
                case '2':
                    $url .= implode('-', $dismantling);
                    $url .= $this->urlLinkStr($param, '-');
                    break;
                case '3':
                    $url .= implode('/', $dismantling);
                    $url .= $this->urlLinkStr($param, '/');
                    break;
                case '1':
                default:
                    $url = $urlModel['index'] == '0' ? '/index.php' : '/';
                    $url .= "?g={$dismantling[0]}&m={$dismantling[1]}&a={$dismantling[2]}";
                    $url .= $this->urlLinkStr($param);
            }
        }

        return $url;
    }

    /**
     * URL的链接字符串格式
     * @param type $param 参数内容
     * @param type $str 连接符的格式
     */
    private function urlLinkStr($param, $str = '') {
        if (!empty($str)) {
            foreach ($param as $key => $value) {
                $url .= "{$str}{$key}{$str}{$value}";
            }
        } else {
            foreach ($param as $key => $value) {
                $url .= "&{$key}={$value}";
            }
        }
        return $url;
    }

    /**
     * 文件下载
     * @param type $file 文件列表
     * @return type 返回一个数组
     */
    public function dowonload($file) {
        $list = explode(',', $file);
        return $list;
    }

}
